package com.xiaoma.tpo.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xiaoma.tpo.dao.TRightDao;
import com.xiaoma.tpo.dao.TRoleRightDao;
import com.xiaoma.tpo.domain.TRight;
import com.xiaoma.tpo.domain.TRoleRight;
import com.xiaoma.tpo.dto.TRightVO;

@Service
public class TRightService {
	@Autowired
	private TRightDao tRightDao;
	@Autowired
	private TRoleRightDao tRoleRightDao;

	public List<TRightVO> getRightLeftTree() {
		List<TRight> lTRights = tRightDao.findAll();
		List<TRightVO> pTRightVOs = new ArrayList<TRightVO>();
		for(TRight tRight: lTRights){
			TRightVO tRightVO = new TRightVO();
			tRightVO.setId(tRight.getId());
			tRightVO.setpId(tRight.getParentId());
			tRightVO.setName(tRight.getRightName());
			tRightVO.setUrlContent(tRight.getUrl());
			tRightVO.setDescription(tRight.getDescription());
			pTRightVOs.add(tRightVO);
		}
		return pTRightVOs;
	}

	public List<TRightVO> getRightLeftTreeById(Integer id) {
		
		//递归查询
		List<TRightVO> pTRightVOs =  getRightById(id);
		return pTRightVOs;
	}
	//递归查询相应的id下的所有节点的方法
	public List<TRightVO> getRightById(Integer id){
		TRight tRight = new TRight();
		tRight.setParentId(id);
		List<TRight> lTRights =  tRightDao.findByColumns(tRight);
		List<TRightVO> pTRightVOs = new ArrayList<TRightVO>();
		for(TRight tRight1: lTRights){
			TRightVO tRightVO = new TRightVO();
			tRightVO.setId(tRight1.getId());
			tRightVO.setpId(tRight1.getParentId());
			tRightVO.setName(tRight1.getRightName());
			tRightVO.setUrlContent(tRight1.getUrl());
			tRightVO.setDescription(tRight1.getDescription());
			pTRightVOs.add(tRightVO);
		}
		for(TRightVO tRightVO2: pTRightVOs){
			List<TRightVO> lTRights2 = getRightById(tRightVO2.getId());
			if(lTRights2!=null && lTRights2.size() > 0){
				tRightVO2.getChildren().addAll(lTRights2);
			}
		}
		return pTRightVOs;
	}
	//增加权限
	public Integer save(TRightVO tRightVO) {
		TRight tRight = new TRight();
		tRight.setParentId(tRightVO.getpId());
		tRight.setRightName(tRightVO.getName());
		tRight.setUrl(tRightVO.getUrlContent());
		tRight.setDescription(tRightVO.getDescription());
		Integer i = tRightDao.insert(tRight);
		return i;
		
	}
	//编辑权限
	public void edit(TRightVO tRightVO) {
		TRight tRight = new TRight();
		tRight.setId(tRightVO.getId());
		List<TRight> lTRights = tRightDao.findByColumns(tRight);
		if(lTRights.size()>0 && lTRights != null){
			TRight rTRight = lTRights.get(0);
			rTRight.setParentId(tRightVO.getpId());
			rTRight.setRightName(tRightVO.getName());
			rTRight.setUrl(tRightVO.getUrlContent());
			rTRight.setDescription(tRightVO.getDescription());
			tRightDao.update(rTRight);
		}
		
	}
	//递归删除相应ID下的所有节点的ID
	public void delete(Integer id) {
		TRight tRight = tRightDao.getById(id);
		deleteAll(tRight);
		tRightDao.delete(id);
	}
	//递归删除
	public void deleteAll(TRight tRight){
		TRight tRight1 = new TRight();
		tRight1.setParentId(tRight.getId());
		List<TRight> lTRights = tRightDao.findByColumns(tRight1);
		for(TRight tRight2 : lTRights){
			deleteAll(tRight2);
			tRightDao.delete(tRight2.getId());
		}
	}

	public boolean hasRelation(Integer rightId) {
		TRoleRight tRoleRight = new TRoleRight();
		tRoleRight.setRightId(rightId);
		List<TRoleRight> lTRoleRights = tRoleRightDao.findByColumns(tRoleRight);
		if(lTRoleRights != null && lTRoleRights.size() >0){
			return true;
		}
		return false;
	}

	public boolean validateRepeat(TRightVO tRightVO) {
		TRight entity = new TRight();
		entity.setParentId(tRightVO.getpId());
		entity.setRightName(tRightVO.getName());
		List<TRight> lTRights = tRightDao.findByColumns(entity);
		if(lTRights != null && lTRights.size() >0){
			//如果id不为空则 是编辑
			if(tRightVO.getId() != null ){
				if(!lTRights.get(0).getId().equals(tRightVO.getId())){
					return true;
				}
				return false;
			}
			return true;
		}
		return false;
	}
}
